package cn.iocoder.yudao.module.handguard.controller.admin.formula;

import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.handguard.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.user.MiniUserMapper;
import cn.iocoder.yudao.module.handguard.tool.formula.FormulaStrategyFactory;
import cn.iocoder.yudao.module.handguard.tool.formula.IFormulaStrategy;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.Get;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

@Tag(name = "管理后台 - 产品准入项数据")
@RestController
@RequestMapping("/hutai/formula")
@Validated
public class AdminFormulaController {

    @Resource
    private FormulaStrategyFactory formulaStrategyFactory;

    @Resource
    private MiniUserMapper userMapper;

    @GetMapping("/get/{type}")
    @Operation(summary = "产品准入项数据查询")
    public @ResponseBody CommonResult<Map<String, Object>> formulaStrategy(@PathVariable("type") String type) {
        IFormulaStrategy formulaInstance = formulaStrategyFactory.getFormulaInstance(type);
        if (formulaInstance == null) {
            return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), String.format("类型 %s 不存在", type));
        }
        Long userId = getLoginUser().getId();
        UserDO userDO = userMapper.getUserInfo(userId);
        Map<String, Object> map = formulaInstance.generateFormula();
        if (userDO.getUserType().equals("2")){
            // 填充map
            for (String key : map.keySet()) {
                if (key.equals("data")){
                    if (map.get(key).getClass().isArray() ) {
                        String [] arr = (String[]) map.get(key);
                        arr = getFormArr(arr);
                        map.put("data",arr);
                    }else if (map.get(key) instanceof Collection){
                        map.put("data",map.get(key));
                    }
                }
            }
            return success(map);
        }else {
            // 填充map
            for (String key : map.keySet()) {
                if (key.equals("data")){
                    if (map.get(key).getClass().isArray() ) {
                        String [] arr = (String[]) map.get(key);
                        arr = getFormArrTwo(arr);
                        map.put("data",arr);
                    }else if (map.get(key) instanceof Collection){
                        map.put("data",map.get(key));
                    }
                }
            }
            return success(map);
           // return success(formulaInstance.generateFormula());
        }
    }

    public  String [] getFormArr(String [] arr){
        String[] originalArray = arr;
        String valueToRemove = "不限";
        // 使用ArrayList来收集不移除的值
        ArrayList<String> tempList = new ArrayList<String>();
        for (String s : originalArray) {
            if (!s.equals(valueToRemove)) {
                tempList.add(s);
            }
        }
        // 将ArrayList转换回数组
        String[] newArray = new String[tempList.size()];
        newArray = tempList.toArray(newArray);
        return newArray;
    }

    public  String [] getFormArrTwo(String [] arr){
        String[] originalArray = arr;
        String valueToRemove = "不接受";
        // 使用ArrayList来收集不移除的值
        ArrayList<String> tempList = new ArrayList<String>();
        for (String s : originalArray) {
            if (!s.equals(valueToRemove) || !s.equals("无")) {
                tempList.add(s);
            }
        }
        // 将ArrayList转换回数组
        String[] newArray = new String[tempList.size()];
        newArray = tempList.toArray(newArray);
        return newArray;
    }





}
